﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using IntelligentEscalator.BUS;
using IntelligentEscalator.DAL;
using IntelligentEscalator.ElevatorWebSocket;
using IntelligentEscalator.Models;

namespace IntelligentEscalator.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class KoneHomeController : ControllerBase
    {
        private JwtSettings _jwtSettings;
        JwtToken _jwttoken = new JwtToken();
        IHttpContextAccessor _access;
        IWebSocketManage _webSocketManage;
        public KoneHomeController(IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access, IWebSocketManage webSocketManage
           )
        {
            _jwtSettings = jwtSettingsAccesser.Value;
            _webSocketManage = webSocketManage;
            _access = access;
        }

        /// <summary>
        ///获取首页数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("QueryHomeData")]
        public ResponseMessage QueryHomeData()
        {

            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.GetKoneHomePageData();
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "获取首页数据";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        ///查询提示消息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("QueryPromptData")]
        public ResponseMessage QueryPromptData()
        {

            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.QueryPromptData();
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询提示消息";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        ///查询年检提醒列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("QueryInspectAnnuallyList")]
        public ResponseMessage QueryInspectAnnuallyList()
        {

            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.QueryInspectAnnuallyList();
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询年检提醒";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }


        /// <summary>
        ///查询故障提醒列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("QueryFaultList")]
        public ResponseMessage QueryFaultList()
        {

            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.QueryFaultList();
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询故障提醒";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        ///查询设备预警列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("QueryEarlyWarningList")]
        public ResponseMessage QueryEarlyWarningList()
        {

            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.QueryEarlyWarningList();
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询设备预警";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        ///获取首页区域设备状态数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("QueryAreaDevice")]
        public ResponseMessage QueryAreaDevice(int mainareaid)
        {

            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.GetAreaDevice(mainareaid);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "获取首页区域设备状态";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        ///修改年检已读标记
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateAnnuallyRead")]
        public ResponseMessage UpdateInspectAnnually(int id)
        {

            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            MaintainDAL da = new MaintainDAL();
            try
            {
                message = BUS.UpdateAnnuallyRead(id);
                if (message.errcode == 0)
                {
                    MaintainInfo maintainInfo = da.LoadMaintain(id);
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "年检工单" + maintainInfo.maintenanceNo + "已读";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "修改年检已读标记";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        ///修改预警已读标记
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateElevatorRead")]
        public ResponseMessage UpdateElevatorRead(int id)
        {

            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.UpdateElevatorRead(id);
                if (message.errcode == 0)
                {
                    ElevatorAreaDAL da = new ElevatorAreaDAL();
                    Elevator elevator = da.LoadElevator(id);
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "电梯" + elevator.elevatorName + "预警已读";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "修改预警已读标记";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        /// <summary>
        ///修改故障已读标记
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateFaultRead")]
        public ResponseMessage UpdateFaultRead(int id)
        {

            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.UpdateFaultRead(id);
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "修改故障已读标记";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }

        [HttpPost]
        [Route("VideoLoading")]
        public ResponseMessage VideoLoading(string code)
        {

            ResponseMessage message = new ResponseMessage();
            try
            {
                Elevator elevator = new Elevator();
                string re = HttpClientPost(code, elevator);
                message.data = re;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
            }
            return message;
        }

        public string HttpClientPost(string url, object datajson)
        {
            HttpClient httpClient = new HttpClient();//http对象
            //表头参数
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //转为链接需要的格式
            HttpContent httpContent = new JsonContent(datajson);
            //请求
            HttpResponseMessage response = httpClient.GetAsync(url).Result;
            if (response.IsSuccessStatusCode)
            {
                Task<string> t = response.Content.ReadAsStringAsync();
                if (t != null)
                {
                    return t.Result;
                }
            }
            return "";
        }

        /// <summary>
        ///所有预警已读标记
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("UpdateAllElevatorRead")]
        public ResponseMessage UpdateAllElevatorRead()
        {

            ResponseMessage message = new ResponseMessage();
            HomeBUS BUS = new HomeBUS();
            try
            {
                message = BUS.UpdateAllElevatorRead();
                if (message.errcode == 0)
                {
                    string username = HttpContext.Session.GetString("UserName").ToString();
                    LogBUS logBUS = new LogBUS();
                    OperationLog operationLog = new OperationLog();
                    operationLog.operation = "所有电梯预警一键已读";
                    operationLog.operatorName = username;
                    logBUS.InsertOperationLog(operationLog);
                }
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "";
                message.data = null;
                message.total = 0;
                LogHelper.Log(e.ToString());
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "修改预警已读标记";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
            }
            return message;
        }
    }
}
